<?php
    echo $this->Html->script('ckeditor/ckeditor');
    if (empty($typeId)) {
    ?>
    <div class="section">
        <?php
            echo $this->element('toolbar');
        ?>
        <?php
            if (isset($listTypes)) {
                foreach ($listTypes as $type) {
                    //debug($type);
                    echo '<li>' . $this->Html->link($type['NodeType']['name'], array('action' => 'add', $type['NodeType']['id'])) . '</li>';
                }
            }
        ?>
    </div>
    <?php
    } else { ?>
    <div id="content-header">
        <h1>List Contents</h1>
    </div>
    <div id="breadcrumb">
        <a href="<?php echo $this->Html->url('/admin/')?>" title="Go to Home" class="tip-bottom"><i class="icon-home"></i> Home</a>
        <a href="#" class="current"><?php echo $type['NodeType']['name']?></a>
    </div>
    <div class="container-fluid">

    <div class="row-fluid">
    <div class="span12">
    <?php
        echo $this->Form->create('Node', array('id' => 'paginate','class'=>'form-horizontal', 'action' => 'edit', 'url' => $this->request->here(false), 'type' => 'file',
                'inputDefaults' => array('label' => false, 'format' => array('before', 'label', 'between', 'input', 'after', 'error'))));

        echo $this->Html->script('ckeditor/ckeditor');
        echo $this->element('toolbar');
    ?>										
    <div class="widget-box">        
        <?php
            $fieldsets['node_type_id'] = $this->Form->input('node_type_id', array(
                    'label' => false,
                    'value' => $type['NodeType']['id'],
                    'type' => 'hidden'));
            $fieldsets['term_id'] = $this->Form->input('term_id', array(            
                    'type' => 'select',
                    'options' => $list_terms,
                    'empty' => '-- None --',
                    'label' => array('class' => 'control-label'),
                    'between' => '<div class="controls">',
                    'div' => array('class' => 'control-group'),
                    'after' => '</div>',
                    'escape' => false));
            $fieldsets['node_type_id'] = $this->Form->input('node_type_id', array(
                    'label' => array('class' => 'control-label'),
                    'between' => '<div class="controls">',
                    'div' => array('class' => 'control-group'),
                    'after' => '</div>',
                    'value' => $type['NodeType']['id'],
                    'type' => 'hidden'));

            $fieldsets['user_id'] = $this->Form->input('user_id', array(
                    'label' => array('class' => 'control-label'),
                    'between' => '<div class="controls">',
                    'div' => array('class' => 'control-group'),
                    'after' => '</div>',
                    'value' => $user['id'],
                    'type' => 'hidden'));

            $fieldsets['title'] = $this->Form->input('title', array(
                    'label' => array( 'text'=> 'Title','class' => 'control-label'),
                    'between' => '<div class="controls">',
                    'div' => array('class' => 'control-group'),
                    'after' => '</div>',
                    'type' => 'text'));        
            $fieldsets['is_published'] = $this->Form->input('is_published', array(
                    'label' => array('text'=> 'Show/Hide','class' => 'control-label'),
                    'between' => '<div class="controls">',
                    'div' => array('class' => 'control-group'),
                    'default'=>1,
                    'after' => '</div>',
                    'type' => 'checkbox'));

        ?>
        <?php
            $layout = array(
                array('node_type_id', 'user_id'),
                array('title','term_id'),      
                array('is_published'),

            );
            foreach ($fields as $field) {

                $type = 'text';
                if ($field['Field']['field_type'] == 'img' || $field['Field']['field_type'] == 'file') {
                    $type = 'file';
                }
                if($field['Field']['field_type'] == 'textarea'){
                    $type= 'textarea';
                }
                else if($field['Field']['field_type'] == 'checkbox'){
                    $type= 'checkbox';
                }
                $chosseckeditor = null;

                switch ($field['Field']['use_wyswyg']) {
                    case null:
                        $chosseckeditor =null;
                        break;
                    case 0:
                        $chosseckeditor =null;
                        break;                
                    case 1:
                        $chosseckeditor = 'ckeditor';
                        break;
                }  
                $fieldsets['field_' . $field['Field']['name']] = $this->Form->input('field_' . $field['Field']['name'], array(
                        'label' => array('text'=>$field['Field']['label'],'class' => 'control-label'),
                        'between' => '<div class="controls">',
                        'div' => array('class' => 'control-group'),
                        'after' => '</div>',
                        'class'=> $chosseckeditor,
                        'type' => $type));
                $layout[] = array('field_' . $field['Field']['name']);
            }

            $layout = array(
                array(
                    'Basic setting',
                    $layout,
                )
            );
            echo $this->Layout->form($layout, $fieldsets); 
        ?>    
        <?php if(isset($images) && !empty($images)){?>
            <div class="control-group add-more">
                <label class="control-label" for="NodeFieldImage">Image</label>
                <div class="controls">
                    <?php echo $this->Form->input('ProImage', array(
                                'class'=>'file-upload',
                                'label' => false,
                                'type'  => 'file', 
                                'name' => 'data[ProImage][]'));?>

                </div>

            </div>	
            <div id="cloneImage"></div>
            <a style="padding-left: 10px; clear: both; display: block;" href="javascript:add_more();"><?php echo __d("system", "Add more image")?></a>                                                 
            <?php }?>    
        <?php
            echo $this->Form->hidden('Paginate.task', array('id' => 'task', 'value' => ''));
            echo $this->Form->end();
        ?>
        <?php if($typeId==News):?>
            <div class="wd-group-user">
                <div class="wd-email">
                    <div class="wd-input">
                        <label><span> <?php echo __d('system','Choose Group');?>:</span></label>
                        <?php echo $this->Form->input('groupUser',array(
                                    'type'=>'select',
                                    'id'=>'PaginateGroupUser',
                                    'div'=>false,
                                    'label'=>false,
                                    'options'=>$groupUser
                                ));
                        ?>
                    </div>
                </div>                     
                <label><span><?php echo __d('system','Danh sách email');?>:</span></label>
                <?php if(!empty($list_email)):?>
                    <ul class="list-user-email"> 
                        <li class="check_all">                            
                            <input type="checkbox" value="" class="input-chk-all"><?php echo __d('system','Check All');?>                                         
                        </li>
                        <?php foreach($list_email as $k=>$v):?>                       
                            <li class="mail_<?php echo $v['Node']['id'];?>">
                                <input type="checkbox" onclick="add_mail_user('<?php echo $v['Node']['field_email'];?>','<?php echo $v['Node']['id'];?>');" value="<?php echo $v['Node']['field_email'];?>" class="_lst_email_<?php echo $v['Node']['id'];?>"> <?php echo $v['Node']['field_email'];?>                                        
                            </li>                                
                            <?php endforeach;?>
                    </ul>
                    <?php else:?>
                    <?php echo __d('system','Không có email nào !!!');?>
                    <?php endif;?> 

            </div>        
            <div class="wd-group-checked">
                <label><span><?php echo __d('system','List Checked');?></span></label>                               
                <ul class="list-email-checked"></ul>
            </div>
            <?php endif;?>                 								
    </div>    
    <?php }?>
<script >
    $('document').ready(function() {            
            $('.check_all .input-chk-all').live('click',function(){                
                    if($(this).is(':checked')){                        
                        myItems = '<?php echo json_encode($list_email);?>';                    
                        var obj = jQuery.parseJSON(myItems);
                        var lenstr = obj.length;
                        for(objs in obj){
                            var _getId = parseInt(obj[objs]['Node']['id']) ;  
                            var _getEmail = String(obj[objs]['Node']['field_email']) ;                          
                            var _email = '';
                            _email += "<li class=mail_"+_getId+">";
                            _email += "<input type=hidden"; 
                            _email += " name=data[Node][email][]";       
                            _email += " value="+_getEmail;
                            _email += " class=_lst_email_"+_getId;
                            _email += "> ";
                            _email += _getEmail;
                            _email += "<a href='#' onclick=_delete_checked_mail('"+_getId+"')>";
                            _email += " Delete";
                            _email += "</a>";
                            _email += "</li>";
                            $('.list-email-checked').append(_email); 
                            $('.list-user-email .mail_'+_getId+' span').addClass('checked');                         
                        }                             
                    }else{
                      $('.list-email-checked li').remove();
                      $('.list-user-email li div span').removeClass('checked');
                    }                
            })                                                
    });     
</script>
<script type="text/javascript">
    jQuery(document).ready(function($){
            $('<span>Size: 1600x850</span>').insertAfter("#NodeFieldBackgroundImage");
            $('<span>Enter a valid number: (int)</span>').insertAfter("#NodeFieldBackgroundPiority");            
    });
    function add_more(){
        var element= jQuery("#cloneImage").prev();
        jQuery(element).clone().insertBefore("#cloneImage");
        var $this = Green.Elfinder;
        jQuery('form input.file-upload').prop('readonly' , true).click(function(){
                $this.show(jQuery(this));
        });
    }
    function delete_image(id){
        if (confirm("Are you sure you want to delete?")) {
            var href= "<?php echo '/node/node/delete_image/'?>"+id;
            jQuery.ajax({
                    url: href,
                    type: "GET",
                    success: function(data) {
                        jQuery('#image_'+id).remove();
                        jQuery('#image_del_'+id).remove();
                        //alert('Load was performed.');
                    }
            });
        }
    }
</script>
<style type="text/css">
    .wd-group-user,.wd-group-checked{border:none;border-bottom: 1px solid #ccc;border-top: 1px solid #ccc;padding-top: 0px;}
</style>